<template>
	<ul>
		<li v-for="(item, index) in list" :key="index" v-on:touchstart="touchStart($event,index)"
			v-on:touchmove="touchMove($event, index)" v-on:touchend="touchEnd(index)">
			<div class="item-content">{{ item.content }}</div>
			<div class="item-options" :class="{'open': item.open}">
				<div class="option edit" v-on:click="editItem(item)">编辑</div>
				<div class="option delete" v-on:click="deleteItem(index)">删除</div>
			</div>
		</li>
	</ul>
</template>

<script>
	export default {
		props: ['list'],
		data() {
			return {
				startX: null,
				jl: null
			}
		},
		methods: {
			touchStart(e, index) {
				// 记录滑动起始位置
				console.log('开始滑动inedx', e, index)
				// this.startX = e.touches[0].pageX
			},
			touchMove(event, index) {
				// 计算滑动距离并展开删除/编辑按钮
				console.log('计算滑动距离并展开删除/编辑按钮e,index', event, index)
				this.jl = this.startX - event.touches[0].pageX
				// console.log('jl',this.jl)
				if(this.jl>50){
					this.list[index].open = true
				}
			},
			touchEnd(index) {
				// 根据滑动距离确定按钮是否展开
				console.log('根据滑动距离确定按钮是否展开index', index)
			},
			deleteItem(index) {
				// 处理删除按钮点击事件
				console.log('删除inedx', index)
			},
			editItem(item) {
				// 处理编辑按钮点击事件
				console.log('编辑inedx', index)
			}
		}
	}
</script>

<style scoped>
	ul {
		width: 100vw;
	}

	li {
		width: 100%;
		position: relative;
		height: 100rpx;
		border: 2rpx solid red;
		margin-bottom: 20rpx;
	}

	.item-content {
		height: 100rpx;
		padding: 10px;
		background-color: #fff;
	}

	.item-options {
		height: 100rpx;
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		display: flex;
	}

	.item-options.open {
		transform: translateX(-50px);
	}

	.option {
		width: 50px;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 14px;
		color: #fff;
		cursor: pointer;
	}

	.option.edit {
		background-color: #009688;
	}

	.option.delete {
		background-color: #f44336;
	}
</style>